package org.sxf.app.sign.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.ActivityInfo;
import android.os.Bundle;

import com.lidroid.xutils.util.LogUtils;

import org.sxf.app.sign.R;
import org.sxf.app.sign.handler.APPHandler;
import org.sxf.app.sign.widgt.LoadingDialog;
import org.sxf.app.sign.widgt.OnDialogCallBack;

/**
 * Created by zqc on 17/1/13.
 */
public class BaseActivity extends Activity {
    protected LoadingDialog _loadingDialog = null;

    protected void popLoadingDialog(String msgStr){
        dismissLoadingDialog();
        _loadingDialog = new LoadingDialog(this, msgStr);
        _loadingDialog.startLoadingDialog();
    }

    protected void dismissLoadingDialog(){
        if (_loadingDialog != null){
            _loadingDialog.stopLoadingDialog();
        }
    }

    protected void setLoadingDialogTipText(String msgStr){
        if (_loadingDialog != null){
            _loadingDialog.setTipText(msgStr);
        }
    }

    public void showAlertDialog(String title, String detail){
        AlertDialog alertDialog = new AlertDialog.Builder(this).setTitle(title)
                .setMessage(detail)
                .setPositiveButton(getResources().getString(R.string.confirm), new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
//                        APPHandler.getInstance().exit();
                    }
                }).create();
//        alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
//            @Override
//            public void onDismiss(DialogInterface dialogInterface) {
//                APPHandler.getInstance().exit();
//                return;
//            }
//        });
        alertDialog.show();
    }

    public void showAlertDialog(String title, String detail, final int dialogID, final OnDialogCallBack listener){
        AlertDialog alertDialog = new AlertDialog.Builder(this).setTitle(title)
                .setMessage(detail)
                .setPositiveButton(getResources().getString(R.string.confirm), new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        listener.onDialogClickOkButton(dialogID);
                    }
                }).create();
        alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {
                listener.onDialogClickCancelButton(dialogID);
                return;
            }
        });
        alertDialog.show();
    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        // 添加Activity到堆栈
        APPHandler.getInstance().addActivity(this);

//        if (!DataCacheSingleton.getInstance().isStartFromFirstPage()){
//
//            finish();
//            APPHandler.getInstance().exit();
//        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 结束Activity&从栈中移除该Activity
        APPHandler.getInstance().removeActivity(this);
    }

    @Override
    public void onLowMemory() {
        LogUtils.d("onLowMemory");
        APPHandler.getInstance().exit();
        finish();
    }

    @Override
    protected void onStart() {
        super.onStart();
        APPHandler.getInstance().setCurrentActivity(this);
    }

    public void showExist(String title, String detail){
        AlertDialog alertDialog = new AlertDialog.Builder(this).setTitle(title)
                .setMessage(detail)
                .setPositiveButton(getResources().getString(R.string.confirm), new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        APPHandler.getInstance().exit();
                    }
                }).create();
        alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {
                APPHandler.getInstance().exit();
                return;
            }
        });
        alertDialog.show();

//        UIUtils.showExist(this, title, detail);
    }

    protected void exist(){
        AlertDialog alertDialog = new AlertDialog.Builder(this).setTitle(getResources().getString(R.string.tip))
                .setMessage(getResources().getString(R.string.isExist_tip))
                .setPositiveButton(getResources().getString(R.string.confirm), new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        APPHandler.getInstance().exit();
                    }
                }).setNegativeButton(getResources().getString(R.string.cancel),
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                return;
                            }
                        }).create();
        alertDialog.show();
    }

}
